package cn.gjxblog.mq.pattern.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/4 11:48
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("www.alserver.com");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        //2 创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建channel
        Channel channel = connection.createChannel();

        //4 通过channel发送数据
        String msg = "Hello RabbitMQ!";
        channel.basicPublish("","test001",null,msg.getBytes());

        //5 关闭相关连接
        channel.close();
        connection.close();
    }
}
